package cloud.tianai.mate.captcha.generator.impl;

import cloud.tianai.captcha.generator.AbstractImageCaptchaGenerator;
import cloud.tianai.captcha.generator.ImageTransform;
import cloud.tianai.captcha.generator.common.model.dto.*;
import cloud.tianai.captcha.generator.common.util.CaptchaImageUtils;
import cloud.tianai.captcha.interceptor.CaptchaInterceptor;
import cloud.tianai.captcha.resource.ImageCaptchaResourceManager;
import cloud.tianai.captcha.resource.common.model.dto.Resource;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;

import java.awt.image.BufferedImage;

import static cloud.tianai.mate.captcha.common.constant.MateCaptchaTypeConstant.ROTATE_DEGREE;

/**
 * @Author: 天爱有情
 * @date 2023/2/3 11:48
 * @Description 旋转角度图片验证码生成器
 */
public class MateRotateDegreeImageCaptchaGenerator extends AbstractImageCaptchaGenerator {

    @Getter
    @Setter
    private boolean templateRandomDegree = true;

    public MateRotateDegreeImageCaptchaGenerator(ImageCaptchaResourceManager imageCaptchaResourceManager) {
        super(imageCaptchaResourceManager);
    }

    public MateRotateDegreeImageCaptchaGenerator(ImageCaptchaResourceManager imageCaptchaResourceManager, ImageTransform imageTransform, CaptchaInterceptor interceptor) {
        super(imageCaptchaResourceManager);
        setImageTransform(imageTransform);
        setInterceptor(interceptor);
    }

    @Override
    protected void doInit() {
//        ResourceStore resourceStore = imageCaptchaResourceManager.getResourceStore();
//        // 添加一些系统的资源文件
//        resourceStore.addResource(ROTATE_DEGREE, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_RESOURCE_PATH.concat("/rotate/1.png"), DEFAULT_TAG));

    }
    @Override
    public void doGenerateCaptchaImage(CaptchaExchange captchaExchange) {
        GenerateParam param = captchaExchange.getParam();
        Resource resourceImage = requiredRandomGetResource(param.getType(), param.getBackgroundImageTag());
        BufferedImage background = getResourceImage(resourceImage);
        // 随机x， 转换为角度
        int randomX = randomInt(background.getWidth() / 3, background.getWidth() - 10);
        double degree = 360d - randomX / ((background.getWidth()) / 360d);
        BufferedImage rotateImage = CaptchaImageUtils.rotateImage(background, degree);
        captchaExchange.setBackgroundImage(rotateImage);
        captchaExchange.setResourceImage(resourceImage);
        RotateData rotateData = new RotateData();
        rotateData.degree = degree;
        rotateData.randomX = randomX;
        captchaExchange.setTransferData(rotateData);
//        return wrapRotateCaptchaInfo(degree, randomX, rotateImage, param, resourceImage, data);
    }

    public static class RotateData {
        double degree;
        int randomX;
    }

    @SneakyThrows
    @Override
    public ImageCaptchaInfo doWrapImageCaptchaInfo(CaptchaExchange captchaExchange) {
//        if ("jpg".equals(param.getBackgroundFormatName()) || "jpeg".equals(param.getBackgroundFormatName())) {
//            param.setBackgroundFormatName("png");
//        }
        GenerateParam param = captchaExchange.getParam();
        BufferedImage backgroundImage = captchaExchange.getBackgroundImage();
        Resource resourceImage = captchaExchange.getResourceImage();
        CustomData customData = captchaExchange.getCustomData();
        RotateData rotateData = (RotateData) captchaExchange.getTransferData();
        ImageTransformData transform = getImageTransform().transform(param, backgroundImage, null, resourceImage, null, customData);
        RotateImageCaptchaInfo result = RotateImageCaptchaInfo.of(rotateData.degree,
                rotateData.randomX,
                transform.getBackgroundImageUrl(),
                transform.getTemplateImageUrl(),
                resourceImage.getTag(),
                null,
                backgroundImage.getWidth(), backgroundImage.getHeight(),
                null, null
        );
        result.setType(ROTATE_DEGREE);
        return result;
    }

}
